package com.tanhua.server.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.server.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

/**
 * @description:
 * @author: Administrator
 * @date: Created in 2021/2/15 13:57
 * @version: 0.0.1
 * @modified By:
 */
@Service
@Slf4j
public class UserService {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${tanhua.sso.url}")
    private String ssoUrl;

    private static final ObjectMapper MAPPER=new ObjectMapper();
    /**
     * @description 通过sso的rest接口查询
     * @author
     * @date
     * @param
     *@return
     */
    public User queryUserByToken(String token) {
        String url=ssoUrl+"/user/"+token;
        try {
            String data = this.restTemplate.getForObject(url, String.class);
            return MAPPER.readValue(data,User.class);
        } catch (Exception e) {
            log.error("校验token出错~ token="+token,e);
        }
        return null;
    }
}
